/*
Copyright 2011 Raymond Giorgi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package edu.pitt.cs3530.regression;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import edu.pitt.cs3530.configuration.FrontEndConfiguration;
import edu.pitt.cs3530.frontend.DeleteOldFilesThread;
import edu.pitt.cs3530.regression.TimeStamp.SecMSec;

public class DeletionOfOld extends Thread {
	public static String dir = "~/temp/";

	public DeletionOfOld(String dir) {
		if (dir == null) {
			dir = DeletionOfOld.dir;
		}
		if (!(new File(dir)).exists()) {
			(new File(dir)).mkdir();
		}
		for (File file : (new File(dir)).listFiles()) {
			if (file.getName().startsWith("fin")) {
				file.delete();
			}
		}
		List<String> list = this.getTestOne("");
		int i = 1;
		for (String string : list) {
			System.out.println(string);
			System.out.println(i++);
		}
		try {
			write(list, dir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.currentTimeMillis();
		Thread thread = (new DeleteOldFilesThread(dir, 1000,
				FrontEndConfiguration.getInstance().getTimeToKeepRequests()));
		thread.start();
		try {
			Thread.sleep(30000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		thread.stop();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String dir = null;
		if (args.length > 0) {
			dir = args[0];
		}
		new DeletionOfOld(dir);
	}

	public void write(List<String> files, String directory) throws IOException {
		BufferedWriter bw = null;
		FileWriter fw = null;
		for (String string : files) {
			fw = new FileWriter(directory + string);
			bw = new BufferedWriter(fw);
			bw.write("");
			fw.close();
			bw.close();
		}
	}

	public void createTimeStamps(List<String> list)
			throws FileNotFoundException {
		BufferedWriter writer = null;
		FileWriter fw = null;
		for (String string : list) {
			try {
				fw = new FileWriter(string);
				writer = new BufferedWriter(fw);
				writer.write("");
				fw.close();
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public List<String> getTestOne(String directory) {
		List<String> fileNames = new LinkedList<String>();
		Random rand = new Random();
		String fileName = "";
		for (int i = 0; i < 10; i++) {
			SecMSec secMSec = TimeStamp.createSecUSec();
			fileName = directory + "fin-" + secMSec.getSec() + "-"
					+ getRandomUSec(secMSec.getMSec()) + "-";
			long finSec = secMSec.getSec() + Math.abs(rand.nextInt() % 3);
			long finMSec = secMSec.getMSec() + Math.abs(rand.nextInt() % 100);
			if (finMSec > 999) {
				finMSec -= 1000;
			}
			fileName += finSec + "-" + getRandomUSec(finMSec);
			fileNames.add(new String(fileName));
			System.out.println(System.currentTimeMillis());
			System.out.println(fileName);
			try {
				Thread.sleep(Math.abs(rand.nextLong() % 1000));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			fileName = "";
		}
		for (int i = 0; i < 10; i++) {
			SecMSec secMSec = TimeStamp.createSecUSec();
			long finFirstSec = secMSec.getSec()
					+ (Math.abs(rand.nextInt() % 20));
			long finFirstMSec = secMSec.getMSec();
			long finSecSec = finFirstSec + Math.abs(rand.nextInt() % 3);
			long finSecMSec = finFirstMSec + Math.abs(rand.nextInt() % 100);
			if (finSecMSec > 999) {
				finSecMSec -= 1000;
			}
			fileName += directory + "fin-" + finFirstSec + "-"
					+ getRandomUSec(finFirstMSec) + "-" + finSecSec + "-"
					+ getRandomUSec(finSecMSec);
			fileNames.add(new String(fileName));
			fileName = "";
		}
		return fileNames;
	}

	public long getRandomUSec(long mSec) {
		return (mSec * 1000) + Math.abs((new Random()).nextLong() % 1000);
	}
}
